local boxuipath = ...

local BOXUI = {modules = {}, -- loaded = {},
	modulepath = boxuipath, path = boxuipath:gsub("%.", "/") .. "/"
	}

local index_f = function(t, k)
	return t.modules[k] -- or t.loaded[k]
end

setmetatable(BOXUI, {__index = index_f})

BOXUI.add = function(mod)
	assert(BOXUI.modules[mod.name] == nil, "This will overwrite another module")
	BOXUI.modules[mod.name] = mod
	return mod
end

BOXUI.requireAs = function(path, name)
	local modules = BOXUI.modules
	local mod = require(path)
	local modtype = type(mod)
	-- BOXUI.loaded[path] = mod
	if modtype == "function" then mod(BOXUI) end -- it should call BOXUI.add itself
	if modtype == "table" then modules[name] = mod end
end

BOXUI.require = function(path)
	local name = path:match(".-%.?([^.]+)$")
	if not name then return error("Cannot get module name from path: " .. path) end
	BOXUI.requireAs(path, name)
end

local files = { -- fixme: order is important; dependency first
	"utils", 'theme',
	"container", "list",
	"panel",
	"button", "slider",
}

for i, v in ipairs(files) do BOXUI.requireAs(boxuipath .. "." .. v, v) end

return BOXUI

